package cn.hls.web.controller.biz;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.hls.entity.Car;
import cn.hls.entity.CarAttach;
import cn.hls.entity.vo.Msg;
import cn.hls.entity.vo.PageInfo;
import cn.hls.service.CarAttachService;
import cn.hls.service.CarService;
import cn.hls.web.controller.base.BaseController;

/**
 * @fileName: CarAttachController.java
 * @package: cn.hls.web.controller.biz
 * @Description: 车辆基本信息COntroller
 * @author: yinghui.zhang
 * @email: uuhui@163.com
 * @date: 2015-7-18
 */
@Controller
@RequestMapping("/carAttach")
public class CarAttachController extends BaseController<CarAttach> {
	@Resource(name = "carAttachService")
	private CarAttachService service;
	@Resource(name="carService")
	private CarService carService;
	/**
	 * 表单
	 * 
	 * @param id
	 * @param show
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/info")
	public ModelAndView info(Long id, boolean show,Long carId )
			throws Exception {
		CarAttach entity = null;
		ModelAndView modelAndView=new ModelAndView();
		if (id != null) {
			entity = service.find(id);
		}
		if(carId!=null){//获取车辆信息
			modelAndView.addObject("car",carService.find(carId));
		}
		modelAndView.addObject("entity",entity);
		if(show){
			modelAndView.setViewName("carAttach/show");
		}else{
			modelAndView.setViewName("carAttach/info");
		}
		return modelAndView;
	}

	/**
	 * 添加操作
	 * 
	 * @param entity
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Msg add(CarAttach entity) throws Exception {
		if(!service.exists(entity)){
			boolean flag = service.add(entity);
			if (flag) {
				msg.setInfo("操作成功");
				msg.setStatus(200);
			} else {
				msg.setInfo("操作失败");
				msg.setStatus(201);
			}
		}else{
			msg.setInfo("存在相同的附件信息!");
			msg.setStatus(201);
		}
		return msg;
	}

	 
	/**
	 * 删除操作
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/del")
	public Msg del(String ids) throws Exception {
		String[] tempIds = ids.split("\\,");
		service.delete(tempIds);
		return msg;
	}

	/**
	 * 修改操作
	 * @param entity
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/update")
	@ResponseBody
	public Msg update(CarAttach entity) throws Exception {
		boolean flag = service.update(entity);
		if (flag) {
			msg.setInfo("操作成功");
			msg.setStatus(200);
		} else {
			msg.setInfo("操作失败");
			msg.setStatus(201);
		}
		return msg;
	}

	/**
	 * 分页查询
	 * 
	 * @param entity 查询的会友对象
	 * @param page 会友分页对象
	 * @param nodeIsLeaf 判断选择的节点是否分叶子节点
	 * @return PageInfo<CarAttach>
	 * @throws Exception
	 */
	@RequestMapping("/list")
	@ResponseBody
	public PageInfo<CarAttach> list(CarAttach entity, PageInfo<CarAttach> page)
			throws Exception {
		return service.loadCarAttachInfo(entity, page);
	}
	
	@RequestMapping("/view")
	public ModelAndView view(String licenseNum) {
		return new ModelAndView("/carAttach/list").addObject("licenseNum", licenseNum);

	}
  
}
